//
//  MagicUIFactory.m
//  WXReader
//
//  Created by LL on 2020/8/12.
//  Copyright © 2020 Andrew. All rights reserved.
//

#import "TL_UIFactory.h"

@implementation TL_UIFactory

#pragma mark - UIView
+ (UIView *)view {
    return [self viewWithBackgroundColor:nil cornerRadius:0.0];
}

+ (UIView *)viewWithBackgroundColor:(nullable UIColor *)backgroudColor cornerRadius:(CGFloat)cornerRadius {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = backgroudColor ?: [UIColor clearColor];
    if (cornerRadius > 0) {
        view.layer.cornerRadius = cornerRadius;
        view.layer.masksToBounds = YES;
    }
    return view;
}

#pragma mark UILabel
+ (UILabel *)label {
    return [self labelWithBackgroundColor:nil font:nil textColor:nil];
}

+ (UILabel *)labelWithBackgroundColor:(nullable UIColor *)backgroudColor font:(nullable UIFont *)font textColor:(nullable UIColor *)textColor {
    UILabel *label = [[UILabel alloc] init];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = font ?: [UIFont systemFontOfSize:14.0];
    label.backgroundColor = backgroudColor ?: [UIColor clearColor];
    label.textColor = textColor ?: kBlackColor;
    return label;
}

+ (YYLabel *)yylabel {
    return [self yylabelWithBackgroundColor:nil font:nil textColor:nil];
}

+ (YYLabel *)yylabelWithBackgroundColor:(nullable UIColor *)backgroudColor font:(nullable UIFont *)font textColor:(nullable UIColor *)textColor {
    YYLabel *label = [[YYLabel alloc] init];
    label.userInteractionEnabled = YES;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = font ?: [UIFont systemFontOfSize:14.0];
    label.backgroundColor = backgroudColor ?: [UIColor clearColor];
    label.textColor = textColor ?: kBlackColor;
    label.lineBreakMode = NSLineBreakByCharWrapping;
    return label;
}

#pragma mark UIImageView
+ (UIImageView *)imageView {
    return [self imageViewWithBackgroundColor:nil image:nil cornerRadius:0.0];
}

+ (UIImageView *)imageViewWithBackgroundColor:(nullable UIColor *)backgroundColor image:(nullable UIImage *)image cornerRadius:(CGFloat)cornerRadius {
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.userInteractionEnabled = YES;
    imageView.backgroundColor = backgroundColor ?: [UIColor clearColor];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    imageView.image = image;
    if (cornerRadius > 0) {
        imageView.layer.cornerRadius = cornerRadius;
        imageView.layer.masksToBounds = YES;
    }
    return imageView;
}


#pragma mark - UIControl
#pragma mark UIButton
+ (UIButton *)buttonWithTarget:(nullable id)target action:(nullable SEL)action {
    return [self buttonWithType:UIButtonTypeCustom backgroundColor:nil font:nil textColor:nil target:target action:action];
}

+ (UIButton *)buttonTypeCustomWithTarget:(nullable id)target action:(nullable SEL)action {
    return [self buttonWithType:UIButtonTypeCustom backgroundColor:nil font:nil textColor:nil target:target action:action];
}

+ (UIButton *)buttonTypeCustomWithFont:(nullable UIFont *)font
                             textColor:(nullable UIColor *)textColor
                                target:(nullable id)target
                                action:(nullable SEL)action {
    return [self buttonWithType:UIButtonTypeCustom
                backgroundColor:nil
                           font:font
                      textColor:textColor
                         target:target
                         action:action];
}

+ (UIButton *)buttonWithType:(UIButtonType)type backgroundColor:(nullable UIColor *)backgroudColor font:(nullable UIFont *)font textColor:(nullable UIColor *)textColor target:(nullable id)target action:(nullable SEL)action {
    UIButton *button = [UIButton buttonWithType:type];
    button.backgroundColor = backgroudColor ?: [UIColor clearColor];
    [button setTitleColor:textColor ?: kBlackColor forState:UIControlStateNormal];
    button.adjustsImageWhenHighlighted = NO;
    button.clipsToBounds = YES;
    button.titleLabel.font = font ?: [UIFont systemFontOfSize:14.0];
    if (target && action) {
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return button;
}

#pragma mark UITextField
+ (UITextField *)textField {
    return [self textFieldWithBackgroundColor:nil placeholder:@"" textColor:nil font:nil delegate:nil isNumber:NO];
}

+ (UITextField *)textFieldWithBackgroundColor:(nullable UIColor *)backgroundColor placeholder:(NSString *)placeholder textColor:(nullable UIColor *)textColor font:(nullable UIFont *)font delegate:(nullable id<UITextFieldDelegate>)delegate isNumber:(BOOL)isNumber {
    UITextField *textField = [[UITextField alloc] init];
    textField.backgroundColor = backgroundColor ?: [UIColor clearColor];
    textField.keyboardType = isNumber ? UIKeyboardTypeNumberPad : UIKeyboardTypeDefault;
    textField.font = font ?: [UIFont systemFontOfSize:15.0];
    textField.placeholder = placeholder ?: @"";
    textField.textColor = textColor ?: kBlackColor;
    if (delegate) {
        textField.delegate = delegate;
    }
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    textField.leftViewMode = UITextFieldViewModeAlways;
    return textField;
}


#pragma mark - UIScrollView
+ (UIScrollView *)scrollView {
    return [self scrollViewWithBackgroundColor:nil delegate:nil];
}

+ (UIScrollView *)scrollViewWithBackgroundColor:(nullable UIColor *)backgroundColor delegate:(nullable id<UIScrollViewDelegate>)delegate {
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = backgroundColor ?: [UIColor clearColor];
    if (delegate) {
        scrollView.delegate = delegate;
    }
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    return scrollView;
}

#pragma mark UITextView
+ (UITextView *)textView {
    return [self textViewWithBackgroundColor:nil delegate:nil textColor:nil font:nil editable:NO];
}

+ (UITextView *)textViewWithBackgroundColor:(nullable UIColor *)backgroundColor delegate:(nullable id<UITextViewDelegate>)delegate textColor:(nullable UIColor *)textColor font:(nullable UIFont *)font editable:(BOOL)editable {
    UITextView *textView = [[UITextView alloc] init];
    textView.backgroundColor = backgroundColor ?: [UIColor clearColor];
    textView.textColor = textColor ?: kBlackColor;
    textView.font = font ?: [UIFont systemFontOfSize:14.0];
    textView.editable = editable;
    if (delegate) {
        textView.delegate = delegate;
    }
    return textView;
}

#pragma mark UITableView
+ (UITableView *)tableView {
    return [self tableViewWithBackgroundColor:nil delegate:nil style:UITableViewStylePlain cellClass:nil];
}

+ (UITableView *)tableViewWithBackgroundColor:(UIColor *)backgroundColor delegate:(id<UITableViewDataSource,UITableViewDelegate>)delegate style:(UITableViewStyle)style cellClass:(NSArray<Class> *)cellClass {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:style];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = backgroundColor ?: [UIColor clearColor];
    tableView.estimatedRowHeight = 45.0;
    tableView.rowHeight = UITableViewAutomaticDimension;
    if (delegate) {
        tableView.delegate = delegate;
        tableView.dataSource = delegate;
    }
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    for (Class obj in cellClass) {
        [tableView registerClass:obj forCellReuseIdentifier:NSStringFromClass(obj)];
    }
    return tableView;
}

#pragma mark UICollectionView
+ (UICollectionView *)collectionViewWithLayout:(UICollectionViewLayout *)layout {
    return [self collectionViewWithLayout:layout backgroundColor:nil delegate:nil cellClass:nil];
}

+ (UICollectionView *)collectionViewWithLayout:(UICollectionViewLayout *)layout backgroundColor:(nullable UIColor *)backgroundColor delegate:(nullable id<UICollectionViewDataSource, UICollectionViewDelegate>)delegate cellClass:(nullable NSArray<Class> *)cellClass {
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.backgroundColor = backgroundColor ?: [UIColor clearColor];
    if (delegate) {
        collectionView.dataSource = delegate;
        collectionView.delegate = delegate;
    }
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    for (Class obj in cellClass) {
        [collectionView registerClass:obj forCellWithReuseIdentifier:NSStringFromClass(obj)];
    }
    return collectionView;
}

@end
